home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Printing / NotepadCloneWithPrinting / NotepadCloneWithPrinting.cs next >
Encoding:
Text File  |  2001-01-15  |  7.4 KB  |  212 lines

  1. //-------------------------------------------------------
  2. // NotepadCloneWithPrinting.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class NotepadCloneWithPrinting: NotepadCloneWithFormat
  11. {
  12.      PrintDocument      prndoc = new PrintDocument();
  13.      PageSetupDialog    setdlg = new PageSetupDialog();
  14.      PrintPreviewDialog predlg = new PrintPreviewDialog();
  15.      PrintDialog        prndlg = new PrintDialog();
  16.      string             strPrintText;
  17.      int                iStartPage, iNumPages, iPageNumber;
  18.  
  19.      public new static void Main()
  20.      {
  21.           System.Threading.Thread.CurrentThread.ApartmentState =
  22.                                         System.Threading.ApartmentState.STA;
  23.          
  24.           Application.Run(new NotepadCloneWithPrinting());
  25.      }
  26.      public NotepadCloneWithPrinting()
  27.      {
  28.           strProgName = "Notepad Clone with Printing";
  29.           MakeCaption();
  30.  
  31.           prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
  32.           setdlg.Document = prndoc;
  33.           predlg.Document = prndoc;
  34.           prndlg.Document = prndoc;
  35.  
  36.           prndlg.AllowSomePages = true;
  37.           prndlg.PrinterSettings.FromPage = 1;
  38.           prndlg.PrinterSettings.ToPage = 
  39.                                    prndlg.PrinterSettings.MaximumPage;
  40.      }
  41.      protected override void MenuFileSetupOnClick(object obj, EventArgs ea)
  42.      {
  43.           setdlg.ShowDialog();
  44.      }
  45.      protected override void MenuFilePreviewOnClick(object obj, EventArgs ea)
  46.      {
  47.           prndoc.DocumentName = Text;   // Just in case it's printed
  48.  
  49.           strPrintText = txtbox.Text;
  50.           iStartPage   = 1;
  51.           iNumPages    = prndlg.PrinterSettings.MaximumPage;
  52.           iPageNumber  = 1;
  53.  
  54.           predlg.ShowDialog();
  55.      }
  56.      protected override void MenuFilePrintOnClick(object obj, EventArgs ea)
  57.      {
  58.           prndlg.AllowSelection = txtbox.SelectionLength > 0;
  59.  
  60.           if (prndlg.ShowDialog() == DialogResult.OK)
  61.           {
  62.                prndoc.DocumentName = Text;
  63.  
  64.                     // Initialize some important fields.
  65.  
  66.                switch (prndlg.PrinterSettings.PrintRange)
  67.                {
  68.                case PrintRange.AllPages:
  69.                     strPrintText = txtbox.Text;
  70.                     iStartPage   = 1;
  71.                     iNumPages    = prndlg.PrinterSettings.MaximumPage;
  72.                     break;
  73.  
  74.                case PrintRange.Selection:
  75.                     strPrintText = txtbox.SelectedText;
  76.                     iStartPage   = 1;
  77.                     iNumPages    = prndlg.PrinterSettings.MaximumPage;
  78.                     break;
  79.  
  80.                case PrintRange.SomePages:
  81.                     strPrintText = txtbox.Text;
  82.                     iStartPage   = prndlg.PrinterSettings.FromPage;
  83.                     iNumPages    = prndlg.PrinterSettings.ToPage - 
  84.                                         iStartPage + 1;
  85.                     break;
  86.                }
  87.                     // And commence printing.
  88.  
  89.                iPageNumber  = 1;
  90.                prndoc.Print();
  91.           }
  92.      }
  93.      void OnPrintPage(object obj, PrintPageEventArgs ppea)
  94.      {
  95.           Graphics     grfx   = ppea.Graphics;
  96.           Font         font   = txtbox.Font;
  97.           float        cyFont = font.GetHeight(grfx);
  98.           StringFormat strfmt = new StringFormat();
  99.           RectangleF   rectfFull, rectfText;
  100.           int          iChars, iLines;
  101.  
  102.                // Calculate RectangleF for header and footer.
  103.  
  104.           if (grfx.VisibleClipBounds.X < 0)       // Print preview
  105.                rectfFull = ppea.MarginBounds;
  106.           else                                    // Regular print
  107.                rectfFull = new RectangleF(
  108.                     ppea.MarginBounds.Left - (ppea.PageBounds.Width - 
  109.                               grfx.VisibleClipBounds.Width) / 2,
  110.                     ppea.MarginBounds.Top - (ppea.PageBounds.Height - 
  111.                               grfx.VisibleClipBounds.Height) / 2,
  112.                     ppea.MarginBounds.Width, ppea.MarginBounds.Height);
  113.  
  114.                // Calculate RectangleF for text.
  115.  
  116.           rectfText = RectangleF.Inflate(rectfFull, 0, -2 * cyFont);
  117.  
  118.           int iDisplayLines = (int) Math.Floor(rectfText.Height / cyFont);
  119.           rectfText.Height = iDisplayLines * cyFont;
  120.  
  121.                // Set up StringFormat object for rectanglar display of text.
  122.  
  123.           if (txtbox.WordWrap)
  124.           {
  125.                strfmt.Trimming = StringTrimming.Word;
  126.           }
  127.           else
  128.           {
  129.                strfmt.Trimming = StringTrimming.EllipsisCharacter;
  130.                strfmt.FormatFlags |= StringFormatFlags.NoWrap;
  131.           }
  132.                // For "some pages" get to the first page.
  133.  
  134.           while ((iPageNumber < iStartPage) && (strPrintText.Length > 0))
  135.           {
  136.                if (txtbox.WordWrap)
  137.                     grfx.MeasureString(strPrintText, font, rectfText.Size, 
  138.                                        strfmt, out iChars, out iLines);
  139.                else
  140.                     iChars = CharsInLines(strPrintText, iDisplayLines);
  141.  
  142.                strPrintText = strPrintText.Substring(iChars);
  143.                iPageNumber++;
  144.           }
  145.                // If we've prematurely run out of text, cancel the print job.
  146.  
  147.           if (strPrintText.Length == 0)
  148.           {
  149.                ppea.Cancel = true;
  150.                return;
  151.           }
  152.                // Display text for this page
  153.  
  154.           grfx.DrawString(strPrintText, font, Brushes.Black, 
  155.                           rectfText, strfmt);
  156.  
  157.                // Get text for next page.
  158.  
  159.           if (txtbox.WordWrap)
  160.                grfx.MeasureString(strPrintText, font, rectfText.Size, 
  161.                                   strfmt, out iChars, out iLines);
  162.           else
  163.                iChars = CharsInLines(strPrintText, iDisplayLines);
  164.  
  165.           strPrintText = strPrintText.Substring(iChars);
  166.  
  167.                // Reset StringFormat display header and footer.
  168.           
  169.           strfmt = new StringFormat();
  170.  
  171.                // Display filename at top.
  172.  
  173.           strfmt.Alignment = StringAlignment.Center;
  174.           grfx.DrawString(FileTitle(), font, Brushes.Black, 
  175.                           rectfFull, strfmt);
  176.  
  177.                // Display page number at bottom.
  178.  
  179.           strfmt.LineAlignment = StringAlignment.Far;
  180.           grfx.DrawString("Page " + iPageNumber, font, Brushes.Black, 
  181.                           rectfFull, strfmt);
  182.  
  183.                // Decide whether to print another page.
  184.  
  185.           iPageNumber++;
  186.           ppea.HasMorePages = (strPrintText.Length > 0) && 
  187.                               (iPageNumber < iStartPage + iNumPages);
  188.  
  189.                // Reinitialize variables for printing from preview form.
  190.  
  191.           if (!ppea.HasMorePages)
  192.           {
  193.                strPrintText = txtbox.Text;
  194.                iStartPage   = 1;
  195.                iNumPages    = prndlg.PrinterSettings.MaximumPage;
  196.                iPageNumber  = 1;
  197.           }
  198.      }
  199.      int CharsInLines(string strPrintText, int iNumLines)
  200.      {
  201.           int index = 0;
  202.  
  203.           for (int i = 0; i < iNumLines; i++)
  204.           {
  205.                index = 1 + strPrintText.IndexOf('\n', index);
  206.  
  207.                if (index == 0)
  208.                     return strPrintText.Length;
  209.           }
  210.           return index;
  211.      }
  212. }